layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_choiceshop_price';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
			t.c.data = c.data; // 数据
			
			/**自定义的字段，菜单ID**/
			t.c.sysMenuId = c.sysMenuId;
		},
        load:function () { // 加载控件
        	var h = ''
        		+ '<div class="layui-form layui-form-item" lay-filter="' + this.id + '">'
	        		+ '<div class="layui-col-md12">'
		        		+ '<div class="layui-input-block">'
		        			+ '<button type="button" class="layui-btn layui-btn-primary btn-add-sys_col_all"><i class="layui-icon layui-icon-addition"></i> 添加梯度</button>'
		        		+ '</div>'
	        		+ '</div>'
        		+ '</div>'
            	+ '<div class="layui-form layui-form-item" pane>'
            		+ '<label class="layui-form-label">价格梯度</label>'
            		+ '<div class="layui-input-block" id="'+this.c.tableId+'">'
            			+ this.loadTable()
            		+ '</div>'
        		+ '</div>'
    		+ '</div>';
            $('#'+this.c.id).html(h);
            this.listenBtn();
        },
        loadTable:function () { // 加载表格
            return '<table class="layui-table" style="width: 50%;" lay-skin="row" style="margin:0"><thead>'+this.loadTableOfThead()+'</thead><tbody>'+this.loadTableOfTbody()+'</tbody></table>';
        },
        loadTableOfThead:function () { // 加载表头
            var h = '<thead><tr class="layui-bg-blue">';
            h += '<th style="width:8%; text-align: center;">首次调拨数量</th>';
            h += '<th style="width:8%; text-align: center;">再次调拨数量</th>';
            h += '<th style="width:8%; text-align: center;">折扣</th>';
            h += '<th style="width:8%; text-align: center;">单位</th>';
            h+= '<th style="width:15%; text-align: center;">操作</th></tr></thead>';
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = '<tbody>';
            for(var i=0;i<t.c.data.length;i++){
	            h += '<tr>';
	            h += '<td style="text-align:center;"><input type="number" class="layui-input" data-index="' + i + '" data-type="num" lay-filter="_input_num"  value="' + t.c.data[i].num + '" ></td>';
	            h += '<td style="text-align:center;"><input type="number" class="layui-input" data-index="' + i + '" data-type="numSecond" lay-filter="_input_num_second"  value="' + t.c.data[i].numSecond + '" ></td>';
	            h += '<td style="text-align:center;"><input type="number" class="layui-input" data-index="' + i + '" data-type="percent" lay-filter="_input_percent"  value="' + t.c.data[i].percent + '" ></td>';
	            h += '<td style="text-align:center;"><input type="text" class="layui-input" data-index="' + i + '" data-type="unitName" lay-filter="_input_unitName"  value="' + t.c.data[i].unitName + '" ></td>';
	            if (i == 0) {
	                h += '<td style="text-align:center;"></td>'
				} else {
	                h += '<td style="text-align:center;"><a class="layui-btn layui-btn-danger layui-btn-sm" data-index="' + i + '" lay-filter="remove"><i class="layui-icon layui-icon-delete"></i>移除</a></td>'
				}
                h+= '</tr>';
            }
            
            h += '</tbody>';
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
            // 监听“添加梯度”按钮
            $('#' + t.c.id).find('.btn-add-sys_col_all').on('click',function (e) {
            	
            	if (t.c.data.length >= 5) {
            		layer.msg("最多添加五行");
					return;
				}
            	
            	var row = {};
            	row['num'] = 0;
            	row['percent'] = 0;
            	var c = t.c;
            	c.data.push(row);
            	$('#' + c.id).empty();
            	return t.reload(c);
            });
            
			// 监听“数量”输入框
			$('input[lay-filter="_input_num"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
            
			// 监听“数量”输入框
			$('input[lay-filter="_input_num_second"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
            
			// 监听“折扣”输入框
			$('input[lay-filter="_input_percent"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
            
			// 监听“单位”输入框
			$('input[lay-filter="_input_unitName"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
            
            // 监听“移除”
            $('#' + t.c.tableId).find('a[lay-filter="remove"]').on('click',function (e) {
				var index = $(this).attr('data-index');
				t.c.data.splice(index, 1);
				t.reload(t.c);
			});
            
            // 表单控件重新加载
        	form.render();
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        }
	};
	exports('simple_table_choiceshop_price', object);
});